package com.dnapipeline.modelManage.controller;

import com.dnapipeline.modelManage.entity.PipelineModel;
import com.dnapipeline.modelManage.entity.PipelineModel;
import com.dnapipeline.modelManage.entity.ProfileModel;
import com.dnapipeline.modelManage.repository.PipelineRepository;
import com.dnapipeline.modelManage.repository.ProfileRepository;
import com.dnapipeline.modelManage.service.PipelineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/v1/pipelines")
public class PipelineController {

    @Autowired
    private PipelineService pipelineService;
    @Autowired
    private PipelineRepository pipelineRepository;

    @PostMapping
    public ResponseEntity<PipelineModel> createPipeline(@RequestBody PipelineModel pipelineModel){
        PipelineModel created = pipelineService.create(pipelineModel);
        return ResponseEntity.status(HttpStatus.CREATED).body(created);
    }

    // 更新设备（全量更新）
    @PutMapping("/{pipelineKey}")
    public ResponseEntity<PipelineModel> updatePipeline(@PathVariable String pipelineKey,
                                                    @RequestBody PipelineModel pipelineModel){
        PipelineModel updated = pipelineService.update(pipelineKey, pipelineModel);
        if(updated != null){
            return ResponseEntity.ok(updated);
        }
        return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
    }

    // 删除设备，级联删除关联的ProfileModel
    @DeleteMapping("/{pipelineKey}")
    public ResponseEntity<String> deletePipeline(@PathVariable String pipelineKey){
        pipelineService.delete(pipelineKey);
        return ResponseEntity.ok("pipelineKey deleted successfully");
    }

    // 获取所有ProfileModel
    @GetMapping
    public ResponseEntity<List<PipelineModel>> getAllProfiles() {
        List<PipelineModel> pipelineModels = pipelineRepository.findAll();
        return ResponseEntity.ok(pipelineModels);
    }

    // 根据pipelineKey获取对应的PipelineModel列表
    @GetMapping("/{pipelineKey}")
    public ResponseEntity<List<PipelineModel>> getProfilesByPipelineKey(@PathVariable String pipelineKey) {
        List<PipelineModel> pipelineModels = pipelineRepository.findByPipelineKey(pipelineKey);
        return ResponseEntity.ok(pipelineModels);
    }
} 